// pages/studentPage/studentPage.js
import request from '../../utils/request';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalData: '',
    studentInfo:'',
    navList: ['学习心情', '学习历程','我的老师'] ,
    navIndex: 0,
    selectionTeachers: [],
    features: [],
    moolist:[],
    courselist:[]
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // features
    this.getFeatures();
    var schoolImgs = app.globalData.schoolImgs;
    this.setData({
      schoolImgs: schoolImgs
    })
    // 导航记录
    if(wx.getStorageSync('navIndex')) {
      this.setData({
        navIndex: wx.getStorageSync('navIndex')
      })
    }else {
      this.setData({
        navIndex: 0
      })
    }
// 取微信信息
    this.setData({
      globalData: app.globalData
    })
    console.log(app.globalData)
// 请求主页信息
    var openId = wx.getStorageSync('openId');
    var that = this;
    request("/student/myIndex.home", { "username": openId, "thirdType": "wechat" }, { "method": "POST", "header": { "content-type": "application/x-www-form-urlencoded" } }).then(res => {
      if (res.statusCode == 200 && res.data.errCode == 0) {
      console.log(res)
      this.setData({
        studentInfo: res.data.studentIndex
      })
       
      }
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  // 导航
  navswitch({ currentTarget:{dataset:{index}}}) {
    this.setData({
      navIndex: index
    });
    wx.setStorageSync('navIndex', index)
  },
  // 跳转我的期待地点页面
  toExpected: function () {
    var data = "city=" + this.data.cityName
    wx.navigateTo({
      url: '../expected/expected?' + data
    })
  },
  //  点击到老师详情页面
  toTeacherDetail: function (e) {
    var data = "name=" + e.currentTarget.dataset.name + "&id=" + e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../teacher/teacher?' + data
    })
  },
  //  switch到发现
  swichToDiscover: function () {
    wx.switchTab({
      url: '../discover/discover',
    })
  },
  // banner列表request:参数options:{"cityNo":"1","module":1,"identity":1}
  getBannerList: function (options) {
    var that = this;
    request('/index/bannerList.home', { 'cityNo': '北京市', 'module': options.module, 'identity': options.identity }, { "method": "POST", header: { "content-type": "application/x-www-form-urlencoded" } }).then(res => {
      if (res.statusCode == 200 && res.data.errCode == 0) {
        console.log("banner")
        console.log(res)
        that.setData({
          imgUrls: res.data.list
        })
      }
    })
  },
  // 推荐老师request
  getTeacherList: function (options) {
    var that = this;
    request('/index/teacherList.home', { "cityNo": "北京市", "module": 0 }, { "method": "POST", header: { "content-type": "application/x-www-form-urlencoded" } }).then(res => {
      console.log(res)
      var year = new Date().getFullYear();
      var list = res.data.list, sYear;
      var schoolImgs = that.data.schoolImgs;
      for (var i = 0; i < 9; i++) {
        // 教龄
        if (list[i].startTime) {
          sYear = list[i].startTime.split('-')[0];
          list[i].exp = Number(year) - Number(sYear) + 1 + "年";
        } else {
          list[i].exp = 0;
        }
        // 学校图片
        for (var j = 0, len = schoolImgs.length; j < len; j++) {
          if (list[i].school == schoolImgs[i].name) {
            list[i].schoolImg = schoolImgs[i].url;
          }
        }
      }
      that.setData({
        selectionTeachers: list
      })
    })
  },
  // 特色接口
  getFeatures: function () {
    var that = this;
    var features = app.globalData.features;
    if (features[0].title) {
      this.setData({
        features: features
      })
      return
    }
    request('/index/docList.home').then(res => {
      if (res.statusCode == 200 && res.data.errCode == 0) {
        var data = res.data.list;
        for (var i = 0, len = data.length; i < len; i++) {
          features[i].title = data[i].title;
          features[i].content = data[i].desc;
        }
        that.setData({
          features: features
        })
        app.globalData.features = features;
      }
    })
  }
})